<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/3/8
  Time: 16:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page isELIgnored="false" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<style>
    .but{
        height: 20px;
        width:70px;
        cursor:pointer;
        background-color: rgb(0,150,136);
        display: inline-block;
        text-align: center;
        color: white;
        font-size: 12px;
        margin-left: 55px;
    }
</style>
<html>
<head>
    <title>添加用户</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webapp/css/layui.css">

</head>
<body>
    <div style="margin-left: 20%;margin-top: 8% ">
        <form class="layui-form" action="#" style="width: 500px">
            <input value="${updateUser.id}" id="userId" style="display: none">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" id="username" lay-verify="required|phone" autocomplete="off" class="layui-input" value="${updateUser.username}">
                    </div>
                </div>

            </div>
          <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="password" lay-verify="pass"autocomplete="off" class="layui-input" value="${updateUser.password}">
                </div>
                <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">上次登陆时间</label>
                <div class="layui-input-inline">
                    <input type="text"  lay-verify="pass"autocomplete="off" class="layui-input" readonly value = "<fmt:formatDate value="${updateUser.updateTime}" pattern="yyyy年MM月dd日 HH:mm:ss" />">
                </div>
            </div>

            <div id="roleWarp"style="margin-top: 10px">

            </div>

            <div style="margin-top: 10px">
                <div class="but" onclick="updateUser()">提交</div>
                <div class="but" onclick="cancel()">取消</div>
            </div>
        </form>
    </div>

    <script type="text/javascript" src="${path}/webapp/js/jquery-3.1.1.js"></script>

<script type="text/javascript">
   $(function () {
        var userid="${updateUser.id}";
       $.ajax({
           url:'${path}/sys/admin/getUserByUpdate',
           dataType:'json',
           type: 'post',
           data:{'id':userid},
           success:function (data) {
               var role=data.role;
               var userRoleId=data.userRoleId;
               var checkedArray=new Array();//用户角色
                for (var i=0;i<role.length;i++){
                    //查询数组中是否存在
                    userRoleId.findIndex(function (value) {
                        if(value==role[i].rid){
                            checkedArray.push(role[i]);
                            role.splice(i,1);//根据下标删除数组中元素
                        }
                    })
                }
                //遍历选中的角色
                for (var i=0;i<checkedArray.length;i++){

                           $("#roleWarp").append("<div  style='display: inline-block;font-size: 15px;margin-left: 80px;text-align: center'>\n" +
                               "                    <input type=\"checkbox\" value="+checkedArray[i].rid+" class=\"role\"   style='display: inline-block;height: 17px;width: 17px;margin-right: 3px' checked>"+checkedArray[i].roleNameCn+"\n" +
                               "                </div>")
                }
               //遍历未选中的角色
               for (var i=0;i<role.length;i++){
                   $("#roleWarp").append("<div  style='display: inline-block;font-size: 15px;margin-left: 80px;text-align: center'>\n" +
                                   "                    <input type=\"checkbox\" value="+role[i].rid+" class=\"role\"   style='display: inline-block;height: 17px;width: 17px;margin-right: 3px'>"+role[i].roleNameCn+"\n" +
                                   "                </div>")
               }

           },
           error:function () {

           }

       })
   })
   //修改用户
    function updateUser() {
        var username=$("#username").val();
        var password=$("#password").val();
        var id=$("#userId").val();
        var roleId=new Array();
        $("input[class='role']:checked").each(function(i){
            roleId[i] = $(this).val();
        });
        var falg=true;
        falg=isNotNull(username);
        falg=isNotNull(password);
        falg=isNotNull(roleId);
        if(falg){
            $.ajax({
                url:'${path}/sys/admin/updateUserByAdmin',
                data:{'username':username,
                    'password':password,
                    'roleId':roleId,
                    'id':id
                },
                dataType:'json',
                traditional:true,
                type:'post',
                success:function (data) {
                    if(data==true){
                        alert("修改成功");
                        window.parent.loadHtml('${path}/sys/admin/intoUser')
                        window.close();
                    }else{
                        alert("修改失败");
                    }
                },
                error:function () {
                    alert("服务器忙");
                }
            })
        }else{
            alert("请填写完整");
        }
    }

//取消
    function cancel() {

        window.close();
        window.parent.location.reload();
    }

   function isNotNull(ele){
       if(typeof ele==='undefined'){//先判断类型
           return false;
       }else if(ele==null){
           return false;
       }else if(ele==''){
           return false;
       }
       return true;
   }
</script>
</body>
</html>
